<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌/车系/车名" prop="seriesId" label-width="200px">
        <el-select
          v-model="queryParams.seriesId"
          filterable
          clearable
          remote
          reserve-keyword
          placeholder="请输入关键词"
          :remote-method="remoteMethod"
          :loading="loading">
          <el-option
            v-for="item in seriesDataList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="车型id" prop="seriesId">-->
<!--        <el-input-->
<!--          v-model="queryParams.seriesId"-->
<!--          placeholder="请输入车型id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="typeId" prop="typeId">-->
<!--        <el-input-->
<!--          v-model="queryParams.typeId"-->
<!--          placeholder="请输入typeId"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:carType:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:carType:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:carType:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:carType:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"  :pageName="$options.name" ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="carTypeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" width="90"   v-if="columns[0].visible" show-overflow-tooltip />
      <el-table-column label="品牌" align="center" prop="brandName" width="200"  show-overflow-tooltip />
      <el-table-column label="车系" align="center" prop="fctName"  width="200"  show-overflow-tooltip />
      <el-table-column label="车名" align="center" prop="seriesName"  width="200"  show-overflow-tooltip />
      <el-table-column label="车辆款式" align="center" prop="name" width="400" v-if="columns[1].visible" show-overflow-tooltip />
      <el-table-column label="价格" align="center" prop="price" width="100" show-overflow-tooltip />
      <el-table-column label="在售状态" align="center" prop="type" width="120" show-overflow-tooltip >
        <template slot-scope="scope">
          {{ scope.row.type === 0 ? '在售车型' : '停售车型' }}
        </template>
      </el-table-column>
<!--      <el-table-column label="车型id" align="center" prop="seriesId" v-if="columns[4].visible" show-overflow-tooltip />
      <el-table-column label="typeId" align="center" prop="typeId" v-if="columns[5].visible" show-overflow-tooltip />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:carType:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:carType:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆款式对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="560px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="品牌/车系/车名" prop="seriesId">
          <el-select
            v-model="form.seriesId"
            filterable
            clearable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="remoteMethod"
            :loading="loading">
            <el-option
              v-for="item in seriesDataList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" type="textarea" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input-number v-model="form.price" :min="1" :max="99999999" placeholder="请输入价格" />
        </el-form-item>
<!--        <el-form-item label="typeId" prop="typeId">-->
<!--          <el-input v-model="form.typeId" placeholder="请输入typeId" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCarType, getCarType, delCarType, addCarType, updateCarType } from "@/api/system/carType";
import { listCarSeries, getCarSeries, delCarSeries, addCarSeries, updateCarSeries } from "@/api/system/carSeries";
import {listCarFct} from "@/api/system/carFct";

export default {
  name: "CarType",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆款式表格数据
      carTypeList: [],
      seriesDataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        price: null,
        type: null,
        seriesId: null,
        typeId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 列表的列的显示隐藏设置
      columns:[
                  { key: 0, label: `id`, visible: true },
                  { key: 1, label: `名称`, visible: true },
                  { key: 2, label: `价格`, visible: true },
                  { key: 3, label: `0:在售车型 1：停售车型`, visible: true },
                  { key: 4, label: `车型id`, visible: true },
                  { key: 5, label: `$column.columnComment`, visible: true },
      ],
    };
  },
  created() {
    this.getList();
    //列表分页列的动态显示配置
    var columns = JSON.parse(localStorage.getItem(this.$options.name));
    if(columns){
      this.columns = columns;
    }
  },
  methods: {
    /** 查询车辆款式列表 */
    getList() {
      this.loading = true;
      listCarType(this.queryParams).then(response => {
        this.carTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        price: null,
        type: null,
        seriesId: null,
        typeId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    //远程搜索品牌数据
    remoteMethod(query) {
      let params = {
        seriesName: query
      }
      console.log(params);
      if (query !== '') {
        this.loading = true;
        this.getCarSeriesList(params);
      } else {
        this.seriesDataList = [];
      }
    },
    getCarSeriesList(params) {
      listCarSeries(params).then(response => {
        for(let item of response.rows) {
          item.label =item.brandName + " / " + item.fctName + " / " + item.seriesName;
          item.value = item.seriesId;
        }
        this.seriesDataList = response.rows;
        this.loading = false;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆款式";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.seriesDataList.push({
        label : row.brandName+" / "+row.fctName+" / "+row.seriesName,
        value :  row.seriesId,
      })
      getCarType(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆款式";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCarType(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCarType(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除车辆款式编号为"' + ids + '"的数据项？').then(function() {
        return delCarType(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/carType/export', {
        ...this.queryParams
      }, `carType_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
